$(document).ready(function () {

    $('#data-wrapper select').select0();

    /*查询按钮事件*/
    $('#data-wrapper .btn-4-query').off('click').on('click', function () {
        ajaxSubmitResponseHtm({
            url: 'attrParamAlias/query.htm',
            method: 'POST',
            data: $('#data-wrapper form').serialize()
        });
    });

    /*根据一级类别查询二级类别*/
    $('#data-wrapper select[name="levelOneId"]').off('change').on('change', function () {
        var id = $(this).val();
        var _html = '<option value="">-请选择-</option>';
        if (!id) {
            $('#data-wrapper select[name="levelTwoId"]').html(_html);
            $('#data-wrapper select[name="levelTwoId"]').cLearSelect0();
            $('#data-wrapper select').select0();
            return;
        }
        ajaxSubmitResponseJSON({
            url: 'category/ajaxLevelTwoListByPid.htm/' + id,
            success: function (data) {
                if (data.status == 0) {
                    var o = data.data;
                    if (o && o.length > 0) {
                        for (var i = 0; i < o.length; i++) {
                            _html += '<option value="' + o[i].id + '">' + o[i].name + '</option>';
                        }
                    }
                    $('#data-wrapper select[name="levelTwoId"]').html(_html);
                    $('#data-wrapper select').select0();
                }
            }
        });
    });

    /*新增*/
    $('#data-wrapper .btn-4-add').off('click').on('click', function () {
        ajaxSubmitResponseHtm({
            container: '#pop-wrapper',
            url: 'attrParamAlias/edit.htm',
        });
    });

    /*修改*/
    $('#data-wrapper .btn-4-modify').off('click').on('click', function () {
        var id = $(this).data('id');
        if (!id) {
            tipData({message: '请选择要操作的记录', status: 0});
            return;
        }
        ajaxSubmitResponseHtm({
            container: '#pop-wrapper',
            url: 'attrParamAlias/edit.htm',
            data: 'id=' + id
        });
    });

    /*删除*/
    $('#data-wrapper .btn-4-delete').off('click').on('click', function () {
        var id = $(this).data('id');
        if (!id) {
            tipData({message: '请选择要操作的记录', status: 0});
            return;
        }

        layer.confirm('确认删除？',
            {icon: 3, title: '删除提示', btn: ['确认', '取消']},
            function () {
                ajaxSubmitResponseJSON({
                    url: 'attrParamAlias/delete.htm/' + id,
                    success: function (data) {
                        if (data.status == 0) {
                            $('#data-wrapper .btn-4-query').trigger('click');
                        }
                    }
                });
            }
        );

    });

    /*pageSize变化，从新加载数据*/
    $('#data-wrapper .paging-size-List').off('change').on('change', function () {
        var length = $(this).val();
        $('#data-wrapper form input[name=length]').val(length);
        $('#data-wrapper .btn-4-query').trigger('click');
    });

    /*点击分页按钮，实现翻页功能*/
    $('#data-wrapper .pagination-toolbar a').off('click').on('click', function () {
        var distStart = $(this).data('idx');
        var start = $('#data-wrapper form input[name=start]').val();
        if (start != distStart) {
            $('#data-wrapper form input[name=start]').val(distStart);
            $('#data-wrapper .btn-4-query').trigger('click');
        }
    });

});